package net.bwie.jtp.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.jtp.bean.TrafficPvReport;
import net.bwie.jtp.bean.TrafficUvReport;
import net.bwie.jtp.service.trafficChannelStatsSerice;
import net.bwie.jtp.utils.Dateutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/jtp/traffic")
public class TrafficController {
    public static final String DATE_DEFAULT_VALUE = "2025-05-17";
    //  注入service
    @Autowired
    // TODO: 2025/5/21 自动装载渠流量统计服务实现类 
    private  trafficChannelStatsSerice trafficChannelStatsSerice;
    // todo  获取数据
    // TODO: 2025/5/21 唯一访客请求拦截方法 
    // TODO: 2025/5/21 http://localhost:8080/jtp/traffic/UvCount?date=2025-05-17
    // TODO: 2025/5/21 http://localhost:8080/@RequestMapping("/jtp/traffic")/@RequestMapping("/UvCount")?date=2025-05-17 
    @RequestMapping("/UvCount")
    public String getUvCount(@RequestParam(value ="date",defaultValue=DATE_DEFAULT_VALUE)String date){
        // TODO: 2025/5/21 请求参数日期,初步判断,默认值为当天日期 
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = Dateutil.nowDate();
        }
        // TODO: 2025/5/21 请求服务层trafficChannelStatsSerice,查询数据,调用服务层方法
        List<TrafficUvReport> uvCount = trafficChannelStatsSerice.getUvCount(date);
        // TODO: 2025/5/21 如果没有数据,直接返回空字符串 
        if (null == uvCount || uvCount.isEmpty()){
            return "[]";
        }
        // TODO: 2025/5/21 构建json字符串
        return JSON.toJSONString(uvCount);
    }

    @RequestMapping("/PvCount")
    public  String getPvCount(@RequestParam(value ="date",defaultValue=DATE_DEFAULT_VALUE)String date){
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = Dateutil.nowDate();
        }
        List<TrafficPvReport> pvCount = trafficChannelStatsSerice.getPvCount(date);
        if (null == pvCount || pvCount.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(pvCount);
    }
}
